package com.bak;

import java.awt.*;

public class Bullet {
    public static final int SPEED = Integer.parseInt(PropertiesManager.getValue("BULLET_SPEED"));
    public static final int WIDTH = ResourceManager.bulletD.getWidth();
    public static final int HEIGHT = ResourceManager.bulletD.getHeight();
    private int x;
    private int y;
    private Dir dir;
    private TankFrame tankFrame;
    private Rectangle rectangle;
    private boolean live = true;
    private Group group = Group.BAD;

    public Bullet() {

    }

    public Bullet(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tankFrame = tankFrame;
        this.rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public void paint(Graphics g) {

        if (!isLive()) {
            tankFrame.getBulletList().remove(this);
            return;
        }
        move();
        switch (dir) {
            case U:
                g.drawImage(ResourceManager.bulletU, x, y, null);
                break;
            case D:
                g.drawImage(ResourceManager.bulletD, x, y, null);
                break;
            case L:
                g.drawImage(ResourceManager.bulletL, x, y, null);
                break;
            case R:
                g.drawImage(ResourceManager.bulletR, x, y, null);
                break;
            case LU:
                g.drawImage(ResourceManager.bulletLU, x, y, null);
                break;
            case RU:
                g.drawImage(ResourceManager.bulletRU, x, y, null);
                break;
            case RD:
                g.drawImage(ResourceManager.bulletRD, x, y, null);
                break;
            case LD:
                g.drawImage(ResourceManager.bulletLD, x, y, null);
                break;
        }
        this.rectangle.x = x;
        this.rectangle.y = y;
    }


    public void move() {
        switch (dir) {
            case U:
                y -= SPEED;
                break;
            case D:
                y += SPEED;
                break;
            case L:
                x -= SPEED;
                break;
            case R:
                x += SPEED;
                break;
            case LU:
                x -= SPEED;
                y -= SPEED;
                break;
            case RU:
                x += SPEED;
                y -= SPEED;
                break;
            case RD:
                x += SPEED;
                y += SPEED;
                break;
            case LD:
                x -= SPEED;
                y += SPEED;
                break;
        }

        if (x > TankFrame.GAME_WIDTH || x < 0 || y > TankFrame.GAME_HEIGHT || y < 0) {
            this.live = false;
        }


    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public void collideWith(Tank tank) {
        if (tank.getGroup() == group) {
            return;
        }

        if (this.rectangle.intersects(tank.getRectangle())) {
            tank.die();
            this.die();
            tankFrame.getExpolodeList().add(new Expolode(tank.getX() + Tank.WIDTH / 2 - Expolode.WIDTH / 2,
                    tank.getY() + Tank.HEIGHT / 2 - Expolode.HEIGHT / 2, tankFrame));
        }
    }

    private void die() {
        this.live = false;
    }
}
